package com.sino.androiddesign.recyclerviewdemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.activity_recycler.*

/**
 * 本activity 要实现的效果:
 * 1:多种item 2:网格瀑布流 3:侧滑删除 4: 长按切换位置
 * 5:添加删除效果 6:万能适配器 7:自定义头尾 8:自定义RV
 */
class RecyclerActivity : AppCompatActivity() {
    private var pagerAdapter: RaPagerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)
        initView()
    }

    private fun initView() {
        val titles: Array<String> = arrayOf("多类别item", "网格瀑布流", "侧滑删除","长按切换位置","添加删除效果", "万能适配器","自定义头尾","自定义Rv" ,"刷新动画")

        var fragments: ArrayList<Fragment> = ArrayList()
        fragments.add(MultipleItemFragment())
        fragments.add(GridPoolFragment())
        fragments.add(SkipRemoveFragment())
        fragments.add(SwitchPositionFragment())
        fragments.add(AddOrRemoveAnimFragment())
        fragments.add(UniversalAdapterFragment())
        fragments.add(HeaderAndFooterFragment())
        fragments.add(CustomRvFragment())
        fragments.add(RvAnimationFragment())
        pagerAdapter = RaPagerAdapter(titles, fragments, supportFragmentManager)

        ra_pager_vp.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(ra_tab_tl))
        ra_pager_vp.adapter = pagerAdapter
        ra_tab_tl.setupWithViewPager(ra_pager_vp)


        setSupportActionBar(ra_title_tb)
        ra_title_tb.setNavigationOnClickListener { onBackPressed() }
    }
}


class RaPagerAdapter(private var titles: Array<String>, private var fragments: List<Fragment>, private var manager: FragmentManager) : FragmentPagerAdapter(manager) {
    override fun getItem(position: Int): Fragment {
        return fragments[position]
    }

    override fun getCount(): Int {
        return titles.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }

}
